$(document).ready(function() {
	
	$('#DB_CLASS_LIST li').each(function(index) {
		class_id = $(this).attr('flexyclass_id');
		str = ' \
		<tr> \
			<td colspan="2"> \
				<table class="flexyclass-table" border="0" cellspacing="0"> \
					<tr id="CLASS_' + class_id + '"> \
						<td class="flexyclassname-td"> \
							<a name="' + class_id +
							'" href="/sysmodel/flexyclass/update/' + class_id 
							+ '/"><h4> ' + $(this).html() + '</h4></a> \
						</td> \
						<td class="flexyclassoptions-td"> \
							<a href="/sysmodel/flexyclass/group/add/'+ 
								class_id +
							'/">add group</a> \
							<a href="/sysmodel/flexyclass/field/add/' +
								class_id + '/">add field</a> \
							<a href="#">sort groups</a> \
						</td> \
					</tr> \
				</table> \
			</td> \
		</tr>';
		$('#UI_CLASS_LIST').append(str);
	});
	
	
	$('#DB_GROUP_LIST li').each(function(index) {
		group_id = $(this).attr('group_id');
		str = ' \
		<tr id="GROUP_' + group_id + '"> \
			<td class="groupname-td"> \
				<a href="/sysmodel/flexyclass/group/update/' + 
					group_id + '/"><h5>' + $(this).html() + '</h5></a> \
			</td> \
			<td class="groupoptions-td"> \
				<a href="#">sort fields</a> \
			</td> \
		</tr>';
		$(str).insertAfter('#CLASS_' + $(this).attr('flexyclass_id'));
	});
	
	
	$('#DB_FIELD_LIST li').each(function(index) {
		
		field_id = $(this).attr('field_id');
		field_type = $(this).attr('fieldtype');
		user_options = '';
		
		if (field_type.substring(0,3) == 'NTO') { 
			user_options = 
				'<a href="/sysmodel/flexyclass/field/displayfield/add/' +
					field_id + '/">add display fields </a>';

			user_options += 
				'<a href="/sysmodel/flexyclass/field/extrafield/add/' +
					field_id + '/">add extra fields </a>';
		}
		
		str = ' \
			<tr id="FIELD_' + field_id + '"> \
				<td class="fieldname-td"> \
					<a href="/sysmodel/flexyclass/field/update/' + 
						field_id + '/" title="' + field_type + '">'
						+ $(this).html() + '</a> \
				</td> \
				<td class="fieldoptions-td">' 
						+ user_options + 
				'</td> \
			</tr>';
		$(str).insertAfter('#GROUP_' + $(this).attr('group_id'));
	});
	
	$('#DB_DISPLAY_FIELD_LIST li').each(function(index) {
		parent_field_id = $(this).attr('parent_field_id');
		field_id =  $(this).attr('self_field_id');
		str = ' \
			<tr id="DISPLAY_FIELD_' + field_id + '"> \
				<td class="displayfieldname-td"><span>[' + $(this).html() + ']</span></td> \
				<td class="displayfieldoptions-td"> \
				<a href="/sysmodel/flexyclass/field/displayfield/remove/' 
				+ parent_field_id + '/' + field_id  + '/' + '">remove</a></td> \
			</tr>';
		$(str).insertAfter('#FIELD_' + parent_field_id);
	});
	
	$('#DB_EXTRA_FIELD_LIST li').each(function(index) {
		field_id = $(this).attr('self_field_id');
		str = ' \
			<tr id="EXTRA_FIELD_' + field_id + '"> \
				<td class="extrafieldname-td"><a href="/sysmodel/flexyclass/field/extrafield/update/' 
				 + field_id + '/" title="' + $(this).attr('fieldtype') + '">' + $(this).html() +  '</a></td> \
				<td class="extrafieldoptions-td"></td> \
			</tr>';
		$(str).insertAfter('#FIELD_' + $(this).attr('parent_field_id'));
	});
	

}); //document ready
